package com.mcptt.config.body;

import android.util.Xml;

import com.cloudptt.api.product.log.Log;
import com.mcptt.config.body.info.mcdata.McdataInfoBody;
import com.mcptt.config.body.info.mcptt.McpttInfoBody_NewCall;

import org.xmlpull.v1.XmlPullParser;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;

public class ResourceListsBody {
	private static final String Name_CallType = "entry";
	private static final String Name_Uri = "uri";
	private String calleeUrl = "";
	public ResourceListsBody()
	{
	}
	
	public ResourceListsBody(String calleeUrl)
	{
		this.calleeUrl = calleeUrl;
	}

	public static ResourceListsBody parseXmlStr(String xmlStr) throws Exception
	{
		ResourceListsBody body = new ResourceListsBody();
		ByteArrayInputStream strIs = new  ByteArrayInputStream(xmlStr.getBytes());
		XmlPullParser pullParser = Xml.newPullParser();
		pullParser.setInput(strIs, "UTF-8"); //为Pull解释器设置要解析的XML数据
		int event = pullParser.getEventType();

		while (event != XmlPullParser.END_DOCUMENT){

			switch (event) {

				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					if (Name_CallType.equals(pullParser.getName()))
					{
						body.calleeUrl = pullParser.getAttributeValue("", Name_Uri);
					}
					break;

				case XmlPullParser.END_TAG:
					break;

			}

			event = pullParser.next();
		}

		return body;
	}
	
	public String toString()
	{
		StringBuffer strBuff = new StringBuffer();
		strBuff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append("\r\n");
		strBuff.append("<resource-lists xmlns=\"urn:ietf:params:xml:ns:resource-lists\" xmlns:cc=\"urn:ietf:params:xml:ns:copycontrol\">").append("\r\n");
		strBuff.append("<list>").append("\r\n");
		
		strBuff.append("<entry uri=\"").append(calleeUrl).append("\"  cc:copyControl=\"to\" />").append("\r\n");
		
		strBuff.append("</list>").append("\r\n");
		strBuff.append("</resource-lists>");
		
		try {
			return new String(strBuff.toString().getBytes(),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return strBuff.toString();
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
